import {
  versionList,
  addVersion,
  updateVersion,
  updateInstallmentStatus,
  getChannelAndVersionQuery,
} from '../../services/channel/api';
import {
  message,
} from 'antd';

export default {
  namespace: 'stageConfig',

  state: {

    loading: true,

    versionListData: {
      list: [],
      pagination:{}
    },
    channelVersionMap:{
      channelList:[],
      versionList:[],
    }

  },

  effects: {
    *fetchGetVersionList({payload,callback}, { call, put }) {
      const response = yield call(versionList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            versionListData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                pageSize:response.data.size,
                total: response.data.total,
              }
            }
          },
        });
        if (callback) callback();
      }else {
        message.info(response.message)
      }
    },

    *fetchAddVersion({payload,callback}, { call, put }) {
      const response = yield call(addVersion, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            addVersionData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.info(response.message)
      }

    },

    *fetchUpdateVersion({payload,callback}, { call, put }) {
      const response = yield call(updateVersion, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            updateVersionData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.info(response.message)
      }

    },

    *fetchUpdateInstallmentStatus({payload,callback}, { call, put }) {
      const response = yield call(updateInstallmentStatus, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
           updateStageData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.info(response.message)
      }
    },

    *fetchGetChannelAndVersionQuery({payload,callback}, { call, put }) {
      const response = yield call(getChannelAndVersionQuery, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            channelVersionMap: response.data
          },
        });
        if (callback && response) callback(response);
      }else {
        message.info(response.message)
      }

    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
